package com.dianping.cat.consumer.event.model.transform;

import com.dianping.cat.consumer.event.model.IEntity;
import com.dianping.cat.consumer.event.model.entity.EventName;
import com.dianping.cat.consumer.event.model.entity.EventReport;
import com.dianping.cat.consumer.event.model.entity.EventType;
import com.dianping.cat.consumer.event.model.entity.GraphTrend;
import com.dianping.cat.consumer.event.model.entity.Machine;
import com.dianping.cat.consumer.event.model.entity.Range;
import com.dianping.cat.consumer.event.model.entity.StatusCode;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;














public class DefaultSaxParser
  extends DefaultHandler
{
/*  45 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  47 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  49 */   private Stack<String> m_tags = new Stack();

/*  51 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  55 */   private StringBuilder m_text = new StringBuilder();


/*  58 */   public static EventReport parse(InputStream in) throws SAXException, IOException { return (EventReport)parseEntity(EventReport.class, new InputSource((InputStream)removeBOM(in))); }



/*  62 */   public static EventReport parse(Reader reader) throws SAXException, IOException { return (EventReport)parseEntity(EventReport.class, new InputSource((Reader)removeBOM(reader))); }



/*  66 */   public static EventReport parse(String xml) throws SAXException, IOException { return (EventReport)parseEntity(EventReport.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  72 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  73 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  75 */       factory.setValidating(false);
/*  76 */       factory.setFeature("http://xml.org/sax/features/validation", false);

      factory.newSAXParser().parse(is, handler);
/*  79 */       return (T)handler.getEntity();
/*  80 */     } catch (ParserConfigurationException e) {
/*  81 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


/*  86 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  90 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  96 */     if (obj instanceof String) {
/*  97 */       String str = (String)obj;

/*  99 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/* 100 */         return (T)str.substring(1);
      }
/* 102 */       return obj;
    }
/* 104 */     if (obj instanceof InputStream) {
/* 105 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/* 107 */       in.mark(3);

/* 109 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 110 */         in.reset();
      }

/* 113 */       return (T)in;
/* 114 */     }  if (obj instanceof Reader) {
/* 115 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 117 */       in.mark(1);

/* 119 */       if (in.read() != 65279) {
/* 120 */         in.reset();
      }

/* 123 */       return (T)in;
    }
/* 125 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 131 */     if (value == null || value.length() == 0) {
/* 132 */       return defaultValue;
    }

/* 135 */     if (type == Boolean.class)
/* 136 */       return (T)Boolean.valueOf(value); 
/* 137 */     if (type == Integer.class)
/* 138 */       return (T)Integer.valueOf(value); 
/* 139 */     if (type == Long.class)
/* 140 */       return (T)Long.valueOf(value); 
/* 141 */     if (type == Short.class)
/* 142 */       return (T)Short.valueOf(value); 
/* 143 */     if (type == Float.class)
/* 144 */       return (T)Float.valueOf(value); 
/* 145 */     if (type == Double.class)
/* 146 */       return (T)Double.valueOf(value); 
/* 147 */     if (type == Byte.class)
/* 148 */       return (T)Byte.valueOf(value); 
/* 149 */     if (type == Character.class) {
/* 150 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 152 */     return (T)value;
  }




/* 158 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 163 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 168 */     if (uri == null || uri.length() == 0) {
/* 169 */       Object currentObj = this.m_objs.pop();
/* 170 */       String currentTag = (String)this.m_tags.pop();

/* 172 */       if (currentObj instanceof EventReport) {
/* 173 */         EventReport eventReport = (EventReport)currentObj;

/* 175 */         if ("domain".equals(currentTag)) {
/* 176 */           eventReport.addDomain(getText());
/* 177 */         } else if ("ip".equals(currentTag)) {
/* 178 */           eventReport.addIp(getText());
        }
/* 180 */       } else if (currentObj instanceof EventType) {
/* 181 */         EventType type = (EventType)currentObj;

/* 183 */         if ("successMessageUrl".equals(currentTag)) {
/* 184 */           type.setSuccessMessageUrl(getText());
/* 185 */         } else if ("failMessageUrl".equals(currentTag)) {
/* 186 */           type.setFailMessageUrl(getText());
        }
/* 188 */       } else if (currentObj instanceof EventName) {
/* 189 */         EventName name = (EventName)currentObj;

/* 191 */         if ("successMessageUrl".equals(currentTag)) {
/* 192 */           name.setSuccessMessageUrl(getText());
/* 193 */         } else if ("failMessageUrl".equals(currentTag)) {
/* 194 */           name.setFailMessageUrl(getText());
        }
      }
    }

/* 199 */     this.m_text.setLength(0);
  }


/* 203 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 207 */   protected String getText() { return this.m_text.toString(); }


  private void parseForEventReport(EventReport parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 211 */     if ("domain-names".equals(qName) || "domain".equals(qName) || "ips".equals(qName) || "ip".equals(qName)) {
/* 212 */       this.m_objs.push(parentObj);
/* 213 */     } else if ("machine".equals(qName)) {
/* 214 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 216 */       this.m_linker.onMachine(parentObj, machine);
/* 217 */       this.m_objs.push(machine);
    } else {
/* 219 */       throw new SAXException(String.format("Element(%s) is not expected under event-report!", new Object[] { qName }));
    }

/* 222 */     this.m_tags.push(qName);
  }

  private void parseForGraphTrend(GraphTrend parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 226 */     this.m_objs.push(parentObj);
/* 227 */     this.m_tags.push(qName);
  }

  private void parseForMachine(Machine parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 231 */     if ("type".equals(qName)) {
/* 232 */       EventType type = this.m_maker.buildType(attributes);

/* 234 */       this.m_linker.onType(parentObj, type);
/* 235 */       this.m_objs.push(type);
    } else {
/* 237 */       throw new SAXException(String.format("Element(%s) is not expected under machine!", new Object[] { qName }));
    }

/* 240 */     this.m_tags.push(qName);
  }

  private void parseForName(EventName parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 244 */     if ("successMessageUrl".equals(qName) || "failMessageUrl".equals(qName)) {
/* 245 */       this.m_objs.push(parentObj);
/* 246 */     } else if ("range".equals(qName)) {
/* 247 */       Range range = this.m_maker.buildRange(attributes);

/* 249 */       this.m_linker.onRange(parentObj, range);
/* 250 */       this.m_objs.push(range);
/* 251 */     } else if ("graph-trend".equals(qName)) {
/* 252 */       GraphTrend graphTrend = this.m_maker.buildGraphTrend(attributes);

/* 254 */       this.m_linker.onGraphTrend(parentObj, graphTrend);
/* 255 */       this.m_objs.push(graphTrend);
/* 256 */     } else if ("statusCode".equals(qName)) {
/* 257 */       StatusCode statusCode = this.m_maker.buildStatusCode(attributes);

/* 259 */       this.m_linker.onStatusCode(parentObj, statusCode);
/* 260 */       this.m_objs.push(statusCode);
    } else {
/* 262 */       throw new SAXException(String.format("Element(%s) is not expected under name!", new Object[] { qName }));
    }

/* 265 */     this.m_tags.push(qName);
  }

  private void parseForRange(Range parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 269 */     this.m_objs.push(parentObj);
/* 270 */     this.m_tags.push(qName);
  }

  private void parseForStatusCode(StatusCode parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 274 */     this.m_objs.push(parentObj);
/* 275 */     this.m_tags.push(qName);
  }

  private void parseForType(EventType parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 279 */     if ("successMessageUrl".equals(qName) || "failMessageUrl".equals(qName)) {
/* 280 */       this.m_objs.push(parentObj);
/* 281 */     } else if ("name".equals(qName)) {
/* 282 */       EventName name = this.m_maker.buildName(attributes);

/* 284 */       this.m_linker.onName(parentObj, name);
/* 285 */       this.m_objs.push(name);
/* 286 */     } else if ("graph-trend".equals(qName)) {
/* 287 */       GraphTrend graphTrend = this.m_maker.buildGraphTrend(attributes);

/* 289 */       this.m_linker.onGraphTrend(parentObj, graphTrend);
/* 290 */       this.m_objs.push(graphTrend);
    } else {
/* 292 */       throw new SAXException(String.format("Element(%s) is not expected under type!", new Object[] { qName }));
    }

/* 295 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 299 */     if ("event-report".equals(qName)) {
/* 300 */       EventReport eventReport = this.m_maker.buildEventReport(attributes);

/* 302 */       this.m_entity = eventReport;
/* 303 */       this.m_objs.push(eventReport);
/* 304 */       this.m_tags.push(qName);
/* 305 */     } else if ("machine".equals(qName)) {
/* 306 */       Machine machine = this.m_maker.buildMachine(attributes);

/* 308 */       this.m_entity = machine;
/* 309 */       this.m_objs.push(machine);
/* 310 */       this.m_tags.push(qName);
/* 311 */     } else if ("type".equals(qName)) {
/* 312 */       EventType type = this.m_maker.buildType(attributes);

/* 314 */       this.m_entity = type;
/* 315 */       this.m_objs.push(type);
/* 316 */       this.m_tags.push(qName);
/* 317 */     } else if ("name".equals(qName)) {
/* 318 */       EventName name = this.m_maker.buildName(attributes);

/* 320 */       this.m_entity = name;
/* 321 */       this.m_objs.push(name);
/* 322 */       this.m_tags.push(qName);
/* 323 */     } else if ("range".equals(qName)) {
/* 324 */       Range range = this.m_maker.buildRange(attributes);

/* 326 */       this.m_entity = range;
/* 327 */       this.m_objs.push(range);
/* 328 */       this.m_tags.push(qName);
/* 329 */     } else if ("graph-trend".equals(qName)) {
/* 330 */       GraphTrend graphTrend = this.m_maker.buildGraphTrend(attributes);

/* 332 */       this.m_entity = graphTrend;
/* 333 */       this.m_objs.push(graphTrend);
/* 334 */       this.m_tags.push(qName);
/* 335 */     } else if ("statusCode".equals(qName)) {
/* 336 */       StatusCode statusCode = this.m_maker.buildStatusCode(attributes);

/* 338 */       this.m_entity = statusCode;
/* 339 */       this.m_objs.push(statusCode);
/* 340 */       this.m_tags.push(qName);
    } else {
/* 342 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 348 */     if (uri == null || uri.length() == 0) {
/* 349 */       if (this.m_objs.isEmpty()) {
/* 350 */         parseRoot(qName, attributes);
      } else {
/* 352 */         Object parent = this.m_objs.peek();
/* 353 */         String tag = (String)this.m_tags.peek();

/* 355 */         if (parent instanceof EventReport) {
/* 356 */           parseForEventReport((EventReport)parent, tag, qName, attributes);
/* 357 */         } else if (parent instanceof Machine) {
/* 358 */           parseForMachine((Machine)parent, tag, qName, attributes);
/* 359 */         } else if (parent instanceof EventType) {
/* 360 */           parseForType((EventType)parent, tag, qName, attributes);
/* 361 */         } else if (parent instanceof EventName) {
/* 362 */           parseForName((EventName)parent, tag, qName, attributes);
/* 363 */         } else if (parent instanceof Range) {
/* 364 */           parseForRange((Range)parent, tag, qName, attributes);
/* 365 */         } else if (parent instanceof GraphTrend) {
/* 366 */           parseForGraphTrend((GraphTrend)parent, tag, qName, attributes);
/* 367 */         } else if (parent instanceof StatusCode) {
/* 368 */           parseForStatusCode((StatusCode)parent, tag, qName, attributes);
        } else {
/* 370 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 374 */       this.m_text.setLength(0);
    } else {
/* 376 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }

  protected Date toDate(String str, String format) {
    try {
/* 382 */       return (new SimpleDateFormat(format)).parse(str);
/* 383 */     } catch (ParseException e) {
/* 384 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format) {
    try {
/* 390 */       return (new DecimalFormat(format)).parse(str);
/* 391 */     } catch (ParseException e) {
/* 392 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/event/model/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */